//by k1 2019.3.25 - 2022.1.20 *逃离

	if (!isNil "close_combat_act") exitWith {};
	close_combat_moveOut = {
		unassignVehicle _this;
		_this leaveVehicle (vehicle _this);
		moveOut _this;
	};
	if (isServer) then {
		kk_getFactionSide = {
			private _sideID = getNumber (configFile >> "CfgVehicles" >> typeOf _this >> "side");
			if (_sideID == 0) exitWith {east};
			if (_sideID == 1) exitWith {west};
			if (_sideID == 2) exitWith {independent};
			if (_sideID == 3) exitWith {civilian};
			side _this
		};
		close_combat_sv = {
			params ["_tag", "_ply"];
			_r = 1.5 + random 4.5;
			_t = _tag getVariable ["close_combat_unTime", time];
			_tag setVariable ["close_combat_unTime", _t + _r];
			_dama = damage _tag;
			_hit = (1 + random 2) / 10;
			//_tagSide = if(lifeState _tag == "INCAPACITATED")then[{_tag getVariable ["close_combat_tagSide", side _tag]},{_tag setVariable ["close_combat_tagSide", side _tag, true]; side _tag}]; //
			_tagSide = _tag call kk_getFactionSide;
			_isEnemy = (_ply findNearestEnemy _ply == vehicle _tag); //
			if (isPlayer _tag) then {format["%1 对 %2 发起了近战攻击", name _ply, name _tag] remoteExec ["systemchat", 0]};
			_tag_rating = rating _tag;
			_tag setDamage (_dama + _hit);
			if (_tag getVariable ["close_combat_once", false]) exitWith {};
			_tag setVariable ["close_combat_once", true];
			if (!isNull objectParent _tag) then {
				[_tag, "amovppnemstpsnonwnondnon"] remoteExec ["playMoveNow", 0];
				sleep 1;
				_tag remoteExec ["close_combat_moveOut", _tag];
				_time = time + 5;
				waitUntil {isNull objectParent _tag || {time > _time}};
			};
			if !(lifeState _tag in ["INCAPACITATED", "DEAD"]) then {[_tag, true] remoteExec ["setUnconscious", _tag]};
			_t = time + 1.5; //
			waitUntil {!alive _tag || {time > _t}};
			_tag setVariable ["close_combat_once", false];
			if (!alive _tag) then {
				if ((side _ply == _tagSide || {(side _ply getFriend _tagSide >= .6) && {_tagSide getFriend side _ply >= .6}}) && {rating _ply >= -2000}) then [{ ////
					_ply addPlayerScores [-1,0,0,0,0];
					if (isNil "close_combat_rating") then {close_combat_rating = -1750};
					[_ply, close_combat_rating] remoteExec ["addRating", _ply];
					format["%1 用近战攻击打死友军 评分 %2", name _ply,close_combat_rating] remoteExec ["systemchat", 0];
				},{
					if (_isEnemy || {_tag_rating <= -2000}) then {_ply addPlayerScores [1,0,0,0,0]; [_ply, 500] remoteExec ["addRating", _ply]};
				}];
			};
			if (alive _tag && {_tag getVariable ["close_combat_wu", true]}) then {
				_tag setVariable ["close_combat_wu", false];
				waitUntil {!alive _tag || {time > _tag getVariable ["close_combat_unTime", 0]} || {!isNull objectParent _tag}};
				_tag setVariable ["close_combat_wu", nil];
				_tag setVariable ["close_combat_unTime", nil];
				if (!alive _tag || {!(_tag getVariable ["k1_Only_Damage_EH", true])}) exitWith {}; //
				[_tag, false] remoteExec ["setUnconscious", _tag];
				_move = switch (currentWeapon _tag) do {
					case "": {"amovppnemstpsnonwnondnon"};
					case (primaryWeapon _tag): {"amovppnemstpsraswrfldnon"};
					case (secondaryWeapon _tag): {"amovpknlmstpsraswlnrdnon"};
					case (handGunWeapon _tag): {"amovppnemstpsraswpstdnon"};
					default {"amovppnemstpsnonwnondnon"};
				};
				sleep 2; //
				if (alive _tag && {animationState _tag == "unconsciousrevivedefault"} && {isNull objectParent _tag} && {isNil {_tag getVariable "close_combat_unTime"}}) then {[_tag, _move] remoteExec ["switchMove", 0]}; //debug
			};
		};
		"close_combat_svPV" addPublicVariableEventHandler {(_this select 1) spawn close_combat_sv};
	};
	
	if (isDedicated) exitWith {};
	
	"close_combat_Move" addPublicVariableEventHandler {(_this select 1) params ["_ply","_move"]; _ply switchMove _move};
	
	close_combat_check = {
		_return = false;
		_tag = cursorTarget;
		if (isNull _tag) then {_tag = cursorObject};
		if (alive _tag && {!(_tag isKindOf "CaManBase")}) then { _ins = lineIntersectsSurfaces [eyePos player, AGLToASL screenToWorld [.5,.5], player, objNull, true, 1, "FIRE", "FIRE"]; if (count _ins > 0) then {_insObj = _ins select 0 select 2; if (_insObj isKindOf "CaManBase") then {_tag = _insObj}}; };
		close_combat_tag = _tag;
		if( alive _tag && {_tag isKindOf "CaManBase"} && {(_tag distance player) < 1.5} && {stance player != "PRONE"} && {player == vehicle player} && {_tag getVariable ["FAR_isUnconscious", 0] == 0} && {!isNil "close_combat_sw" || {!isPlayer _tag}} ) then { _return = true };
		_return
	}; //"UNDEFINED" 水里
	close_combat_addKey = {
		waitUntil {!isNull (findDisplay 46)};
		close_combat_KeyDown = (findDisplay 46) displayAddEventHandler ["KeyDown", {
			if (time > close_combat_Key && {_this select 1 == 15} && {call close_combat_check}) then {
				//playSound3D ["音效路径", close_combat_tag];
				close_combat_svPV = [close_combat_tag, player];
				if (isServer) then [{close_combat_svPV spawn close_combat_sv},{publicVariableServer "close_combat_svPV"}];
				player switchMove "AwopPercMstpSgthWnonDnon_end";
				close_combat_Move = [player, "AwopPercMstpSgthWnonDnon_end"];
				publicVariable "close_combat_Move";
				close_combat_Key = time + 1; //
			}; //TAB
		}];
	};
	
	waitUntil {!isNull player};
	player createDiarySubject ["close_combat_help", "近战攻击"];
	player createDiaryRecord ["close_combat_help", ["说明", "<br/>让玩家能空手击杀载具内外的敌人<br/>靠近敌人按TAB键攻击<br/>或者使用鼠标滚轮菜单攻击<br/>连续攻击可以让敌人昏迷时间更长<br/>起码要打几次目标才会死亡<br/>打死友军评分-1750等同于用枪打<br/>打死敌人和-2000的单位评分+500<br/><br/><execute expression = 'systemChat Format[""rating: %1"", rating player];'>玩家评分</execute>  <execute expression = 'if (!isNil {close_combat_KeyDown}) then {(findDisplay 46) displayRemoveEventHandler [""KeyDown"", close_combat_KeyDown]}; 0 spawn close_combat_addKey; systemChat ""已执行"";'>修复按键失效</execute><br/>"]];
	
	if (isServer || {serverCommandAvailable "#kick"}) then {player createDiaryRecord ["close_combat_help", ["设置", "<br/><execute expression = '_str = if(isNil {close_combat_sw})then[{close_combat_sw = 1; ""对玩家有效""},{close_combat_sw = nil; ""对玩家无效""}]; publicVariable ""close_combat_sw""; systemChat Format[""近战攻击: %1"", _str];'>是否对玩家有效（默认无效）</execute><br/>"]]}; /////////
	
	close_combat_Key = 0;
	call close_combat_addKey;
	
	0 spawn {uiSleep 10; systemChat "添加近战攻击"};
	
	0 spawn {
		private ["_ply"];
		close_combat_act = -1;
		while {true} do {
			if (close_combat_act == -1) then {
				_ply = player;
				close_combat_act = _ply addAction ["<img size='1.3' color='#ffffff' shadow='1' image='a3\ui_f\data\IGUI\Cfg\Revive\overlayIconsGroup\f100_ca.paa'/><t color='#FF0000'>近战攻击</t>", {
					if (time > close_combat_Key) then {
						//playSound3D ["音效路径", close_combat_tag];
						close_combat_svPV = [close_combat_tag, player];
						if (isServer) then [{close_combat_svPV spawn close_combat_sv},{publicVariableServer "close_combat_svPV"}];
						player switchMove "AwopPercMstpSgthWnonDnon_end";
						close_combat_Move = [player, "AwopPercMstpSgthWnonDnon_end"];
						publicVariable "close_combat_Move";
						close_combat_Key = time + 1; //
					};
				},"",9354,true,false,"","call close_combat_check"];
			};
			if (_ply != player) then {
				_ply removeAction close_combat_act;
		    	close_combat_act = -1;
			};
			sleep 5;
		};
	};
	
	if (isNil "CBA_Settings_fnc_init") exitWith {};
	[
		"close_combat_keyDebug",
		"CHECKBOX",
		["修复按键功能", "执行后请改回默认值，不然下次开始任务会自动执行此除错操作"],
		"近战攻击",
		false,
		false,
		{ if (close_combat_keyDebug) then {if (!isNil {close_combat_KeyDown}) then {(findDisplay 46) displayRemoveEventHandler ["KeyDown", close_combat_KeyDown]}; 0 spawn close_combat_addKey; systemChat "已执行";} }
	] call CBA_Settings_fnc_init;


